package com.web.servlet;


import com.entity.Stock;
import com.exception.CustomerException;
import com.result.QueryResult;
import com.result.Result;
import com.result.ResultUtils;
import com.service.StockService;
import com.service.impl.StockServiceImpl;
import com.util.JsonUtils;
import com.util.ResponseUtils;
import com.util.WebUtils;
import com.vo.QueryVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "StockServlet",urlPatterns = "/stock/*")
public class StockServlet extends HttpServlet {
    private StockService stockService=new StockServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        String methodName=uri.substring(uri.lastIndexOf("/")+1);
        try{
            if (methodName.equals("list")) {
                list(req, resp);
            }

            if (methodName.equals("add")) {
                add(req, resp);
            }

            if (methodName.equals("update")) {
                update(req, resp);
            }

            if (methodName.equals("upper")) {
                upper(req, resp);
            }

            if (methodName.equals("delete")) {
                delete(req, resp);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new CustomerException("");
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        Integer id=Integer.valueOf(req.getParameter("id"));
        stockService.delete(id);
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),resp);
    }

    private void upper(HttpServletRequest req, HttpServletResponse resp) {
        Integer id=Integer.valueOf(req.getParameter("id"));
        Boolean inUsed=Boolean.valueOf(req.getParameter("inUsed"));
        stockService.upper(id,inUsed);
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Stock stock = WebUtils.requestToBean(req, Stock.class);
        stockService.update(stock);
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        Stock stock = WebUtils.requestToBean(req, Stock.class);
        stock.setInUsed(true);
        stockService.add(stock);
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(ResultUtils.buildSuccess()),resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) {
        QueryVo queryVo = WebUtils.requestToBean(req, QueryVo.class);
        QueryResult queryResult=stockService.selectByPage(queryVo.getStartIndex(),queryVo.getLimit());
        Result result= ResultUtils.buildSuccess(queryResult.getList(),queryResult.getCount());
        ResponseUtils.responseToJson(JsonUtils.writerValueAsString(result),resp);
    }
}
